﻿using System;
using System.Collections;
using System.Web;
using ASPNETVN.PORTAL.Components.Configuration;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.Share.Controls;

namespace ASPNETVN.PORTAL.Components.UI
{
    public class Page : System.Web.UI.Page
    {
        //-----------------------------------------------------------------------------------------

        #region Stardand Controls

        /// <summary>
        /// Standard update button.
        /// </summary>
        protected System.Web.UI.WebControls.LinkButton ButtonUpdate;

        /// <summary>
        /// Standard delete button.
        /// </summary>
        protected System.Web.UI.WebControls.LinkButton ButtonDelete;

        /// <summary>
        /// Standard cancel button.
        /// </summary>
        protected System.Web.UI.WebControls.LinkButton ButtonCancel;

        #endregion

        //-----------------------------------------------------------------------------------------
        public void ReturnToReferrerPage()
        {
            // Redirect back to the portal home page
            if (Request.QueryString["ReturnUrl"] != null)
            {
                Response.Redirect(Server.UrlDecode(Helpers.DecryptHelper.Decrypt(Request.QueryString["ReturnUrl"])));
            }
            else
            {
                string url = ViewState["UrlReferrer"].ToString();
                Response.Redirect(url);
            }
        }

        public void SetTitleText(string text)
        {
            Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)Context.Items["Portal"];
            this.Title = portal.Title + " - " + text;
        }

        public Control FindControlRecursive(Control Root, string Id)
        {
            if (Root.ID == Id)
                return Root;

            foreach (Control Ctl in Root.Controls)
            {
                Control FoundCtl = FindControlRecursive(Ctl, Id);
                if (FoundCtl != null)
                    return FoundCtl;
            }
            return null;
        }

        public void ShowMessage(string caption, string message)
        {
            ((MessageBox)this.FindControlRecursive(this.Master, "PopupMessageBox")).ShowMessage(caption, message);
        }

        public void ShowMessage(string caption, string message, int width, int height)
        {
            ((MessageBox)this.FindControlRecursive(this.Master, "PopupMessageBox")).ShowMessage(caption, message, width, height);
        }

        #region Events

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string master = Global.GetMasterFile();
            this.MasterPageFile = "~/Masters/ASPNETVN.PORTAL.Masters." + master + "/" + master + ".Master";
            this.Theme = Global.GetThemeName();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.ButtonCancel != null)
            {
                this.ButtonCancel.Click += this.ButtonCancel_Click;
                this.ButtonCancel.Text = Global.RM.GetString("BUTTON_GOBACK");
                this.ButtonCancel.CausesValidation = false;
                this.ButtonCancel.EnableViewState = false;
            }
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            // Store URL Referrer to return to portal
            ViewState["UrlReferrer"] = Request.UrlReferrer != null ? Request.UrlReferrer.ToString() : "~/Default.aspx";
        }

        #endregion

        //-----------------------------------------------------------------------------------------

        #region Custom events.

        // Cancel event.
        protected virtual void OnCancel(EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        void ButtonCancel_Click(Object sender, EventArgs e)
        {
            this.OnCancel(e);
        }

        #endregion

        //-----------------------------------------------------------------------------------------

        #region Properties (Tabs)

        private Guid _TabID;

        public Guid TabID
        {
            get
            {
                if (this._TabID == Guid.Empty)
                {
                    if (HttpContext.Current != null && Request.Params["TabID"] != null)
                        this._TabID = new Guid(Request.Params["TabID"]);
                }
                return this._TabID;
            }
        }
       
        #endregion

        //-----------------------------------------------------------------------------------------

        #region Properties (Items)

        private int _itemID;

        public int ItemID
        {
            get
            {
                if (_itemID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["ItemID"] != null)
                        _itemID = Int32.Parse(Request.Params["ItemID"]);
                }
                return _itemID;
            }
            set { _itemID = value; }
        }

        #endregion

        //-----------------------------------------------------------------------------------------

        #region Properties (Modules)

        private Guid moduleID;

        public Guid ModuleID
        {
            get
            {
                if (moduleID == Guid.Empty)
                {
                    if (HttpContext.Current != null && Request.Params["Mid"] != null)
                        moduleID = new Guid(Request.Params["Mid"]);
                }
                return moduleID;
            }
        }

        private Hashtable _settings;

        public Hashtable Settings
        {
            get
            {
                if (this._settings == null)
                {
                    this._settings = moduleID != Guid.Empty ? PortalConfiguration.GetModuleSettings(ModuleID, this) : new Hashtable();
                }

                return this._settings;
            }
        }

        #endregion

        //-----------------------------------------------------------------------------------------

        #region Portal

        public Configuration.Items.Portal Portal
        {
            get
            {
                return (Configuration.Items.Portal)HttpContext.Current.Items["Portal"];
            }
        }

        #endregion
    }
}